# -*- coding: utf-8 -*-
import sys

from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class WidgetGearSystem(QWidget):
    def __init__(self, *args, **kwargs):
        text = kwargs.pop("text", "")
        super(WidgetGearSystem, self).__init__(*args, **kwargs)
        self.InitUI()

    def InitUI(self):
        self.bgcolor_set = QColor(50, 50, 50, 255)
        self.wheel_color = QColor(255, 186, 90)
        self.frame_color = QColor(255, 186, 85)
        self.leftBreakPressPercent = 0.2
        self.rightBreakPressPercent = 0.5

    def paintEvent(self, event):
        width = self.width()
        height = self.height()

        painter = QPainter(self)  # 初始化painter
        painter.translate(width / 2, height / 2)  # 坐标轴变换，调用translate()将坐标原点平移至窗口中心
        painter.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing)

        self.bgWidth = width
        self.bgHeight = height

        painter.begin(self)

        # 画出背景区域
        self.drawBackground(painter)

        # 画出前轮和主轮示意图
        self.drawWheelFrame(painter)

        # 刹车示意图
        self.drawBreakIndicator(painter)

        painter.end()

    def drawWheelFrame(self, painter):
        painter.save()

        painter.setPen(QPen(self.frame_color, 4, Qt.SolidLine))
        painter.drawLine(-0.3 * self.bgWidth, 0.3 * self.bgHeight, 0.3 * self.bgWidth, 0.3 * self.bgHeight)
        painter.drawLine(0, 0.3 * self.bgHeight, 0, -0.3 * self.bgHeight)

        wheel_radious = self.bgWidth * 0.06

        rect = QRectF(-wheel_radious, -wheel_radious, wheel_radious * 2, wheel_radious * 2)
        painter.setPen(Qt.NoPen)
        painter.setBrush(self.wheel_color)

        painter.translate(-0.3 * self.bgWidth, 0.3 * self.bgHeight)
        painter.drawEllipse(rect)

        painter.translate(0.6 * self.bgWidth, 0)
        painter.drawEllipse(rect)

        painter.translate(-0.3 * self.bgWidth, -0.6 * self.bgHeight)
        painter.drawEllipse(rect)

        painter.restore()

    def drawBreakIndicator(self, painter):
        painter.save()
        indicator_width = self.bgWidth * 0.1
        pos_down_x = -0.5 * self.bgWidth
        pos_down_y = 0.4 * self.bgHeight
        pos_up_x = -0.5 * self.bgWidth
        pos_up_y = -0.4 * self.bgHeight
        unprocess_color = QColor(189, 210, 239)
        process_color = QColor(25, 78, 157)

        # 左刹车压力指示条
        ratio = self.leftBreakPressPercent
        if ratio < 0.01:
            ratio = 0.01
        elif ratio > 0.99:
            ratio = 0.99

        rg = QLinearGradient(pos_down_x, pos_down_y, pos_up_x, pos_up_y)
        rg.setColorAt(0, process_color)  # 透明色
        rg.setColorAt(ratio, process_color)
        rg.setColorAt(ratio + 0.05, unprocess_color)
        rg.setColorAt(1, unprocess_color)
        painter.setBrush(rg)
        painter.drawRect(pos_down_x, -pos_down_y, indicator_width, pos_down_y - pos_up_y)

        # 右刹车压力指示条
        ratio = self.rightBreakPressPercent

        if ratio < 0.01:
            ratio = 0.01
        elif ratio > 0.99:
            ratio = 0.99

        lg = QLinearGradient(pos_down_x, pos_down_y, pos_up_x, pos_up_y)
        lg.setColorAt(0, process_color)  # 透明色
        lg.setColorAt(ratio, process_color)
        lg.setColorAt(ratio + 0.05, unprocess_color)
        lg.setColorAt(1, unprocess_color)
        painter.setBrush(lg)
        painter.drawRect(-pos_down_x - indicator_width, -pos_down_y, indicator_width, pos_down_y - pos_up_y)

        painter.restore()

    def drawBackground(self, painter):
        painter.save()

        painter.setPen(Qt.NoPen)
        brush = QBrush(Qt.SolidPattern)
        brush.setColor(self.bgcolor_set)
        painter.setBrush(brush)
        painter.drawRect(-self.bgWidth / 2, -self.bgHeight / 2, self.bgWidth, self.bgHeight)

        painter.restore()

    def setBreakPressPercent(self, press_left, press_right):
        """
        设置刹车压力百分比指示条

        :param press_left: 左刹车压力百分比
        :param press_right: 右刹车压力百分比
        :return:
        """

        self.rightBreakPressPercent = press_right
        self.leftBreakPressPercent = press_left
        self.update()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = WidgetGearSystem()
    demo.setGeometry(100, 100, 400, 200)
    demo.show()

    # demo.setIntValue(6)  # 设置指针指向
    # demo.setFloatValue(5.5)
    # demo.setTitle("Title_____")  # 设置图标题
    sys.exit(app.exec_())
